package admin

import (
	"fmt"
	"gq_volunteer_platform/bean"
	"gq_volunteer_platform/model/admin"
	"gq_volunteer_platform/model/organization"
	"gq_volunteer_platform/model/user"
	"gq_volunteer_platform/model/weixin"
	ServiceAdmin "gq_volunteer_platform/service/admin"

	"github.com/gin-gonic/gin"
)

func MyList(c *gin.Context) {

	var req struct {
		Page           int    `binding:"required,gte=1"`
		Limit          int    `binding:"required,gte=1"`
		Name           string `binding:"max=255"`
		OrganizationId uint   `binding:"max=10000000"`
	}

	if err := c.ShouldBindQuery(&req); err != nil {
		bean.Log.Warning(err.Error())
		c.String(400, err.Error())
		return
	}

	var offset = (req.Page - 1) * req.Limit

	var organization_my_ids, err = ServiceAdmin.MyOrganizationIds(c, req.OrganizationId)
	if err != nil {
		bean.Log.Warning(err.Error())
		c.String(400, err.Error())
		return
	}

	// 查询
	var count int64
	// var list []admin.Admin

	var list []struct {
		ID             uint
		Mobile         string
		Name           string
		OrganizationId uint
		Role           string
		RoleID         uint
		Token          string
		UserId         uint
		CreatedAt      string
		UpdatedAt      string

		UserName         string
		UserProfilePhoto string
		UserMobileNumber string
	}

	var tx = bean.MysqlDb.Model(&admin.Admin{}).
		Select("`admin`.*,`user`.`name` as UserName,`user`.`profile_photo` as UserProfilePhoto,`user`.`mobile_number` as UserMobileNumber").
		Joins("LEFT JOIN `user` on `user`.`id` = `admin`.`user_id`")

	tx.Where("`admin`.`organization_id` in ?", organization_my_ids)

	if req.Name != "" {
		tx = tx.Where("`admin`.`name` like ?", "%"+req.Name+"%")
	}

	// if req.OrganizationId != 0 {
	// 	tx = tx.Where("`organization_id`=?", req.OrganizationId)
	// }

	// 查询总数
	if err := tx.Count(&count).Error; err != nil {
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	}

	// 查询列表
	if err := tx.Limit(req.Limit).Offset(offset).Order("`id` DESC").Find(&list).Error; err != nil {
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	}

	c.PureJSON(200, gin.H{
		"count": count,
		"list":  list,
	})
}

// bind wx
// 扫码绑定微信小程序
func BindWeixinQrCode(c *gin.Context) {
	// 用户输入信息
	var req struct {
		AdminToken string
		EnvVersion string `binding:"required,oneof=release trial develop"`
		Page       string `binding:"required,max=255"`
		Width      uint   `binding:"required,min=280,max=1280"`
	}

	if err := c.ShouldBindQuery(&req); err != nil {
		bean.Log.Error(err)
		c.String(400, err.Error())
		return
	}

	var qr weixin.QRCodeGetReq
	qr.Scene = req.AdminToken
	qr.EnvVersion = req.EnvVersion
	qr.IsHyaline = false
	qr.Page = req.Page
	qr.Width = req.Width
	qr.CheckPath = false

	var by, err = qr.QRCodeGet()
	if err != nil {
		c.String(500, err.Error())
		bean.Log.Warning(err.Error())
		return
	}

	c.Header("Content-Type", "image/jpeg")
	c.Header("Content-Disposition", "attachment; filename=\"bind_qr_code.jpeg\"")

	c.Writer.Write(by)
}

// info
func Info(c *gin.Context) {
	// 获取登录用户对象
	// var one struct {
	// 	ID             uint
	// 	Mobile         string
	// 	Name           string
	// 	OrganizationId uint
	// 	Role           string
	// 	RoleID         uint
	// 	Token          string
	// 	CreatedAt      string
	// 	UpdatedAt      string

	// 	UserName         string
	// 	UserProfilePhoto string
	// 	UserMobileNumber string
	// }

	var one struct {
		admin.Admin
		UserName         string
		UserProfilePhoto string
		UserMobileNumber string
	}

	if err := one.Get(c); err != nil {
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	}

	var two user.User
	if err := bean.MysqlDb.Where("id=?", one.UserId).Limit(1).Find(&two).Error; err != nil {
		bean.Log.Warning(err)
		c.String(400, "没有该用户")
		return
	}
	one.UserName = two.Name
	one.UserProfilePhoto = two.ProfilePhoto
	one.UserMobileNumber = two.MobileNumber

	c.PureJSON(200, one)
}

// 添加+修改管理员信息
func Edit(c *gin.Context) {
	// 用户输入信息
	var req struct {
		ID             uint   `binding:"max=10000000"`
		Name           string `binding:"max=255"`
		Mobile         string `binding:"max=255"`
		Role           string `binding:"max=255"`
		RoleID         uint   `binding:"min=1,max=10000000,oneof=1"` // 界面只能添加站点管理员
		OrganizationId uint   `binding:"min=1,max=10000000"`
		Password       string `binding:"max=255"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		bean.Log.Warning(err)
		c.String(400, err.Error())
		return
	}

	if req.ID == 0 && req.Password == "" {
		bean.Log.Error("密码不能为空")
		c.String(400, "密码不能为空")
		return
	}

	// 验证组织是否存在 预防出现管理员不显示的情况
	var org organization.Organization
	if err := bean.MysqlDb.Where("`id`=?", req.OrganizationId).First(&org).Error; err != nil {
		bean.Log.Warning(err)
		c.String(400, fmt.Sprintf("组织不存在id:%d", req.ID))
		return
	}

	var one admin.Admin
	one.ID = req.ID
	one.Name = req.Name
	one.Mobile = req.Mobile
	one.RoleID = req.RoleID
	one.Role = req.Role
	one.OrganizationId = req.OrganizationId
	one.Password = req.Password

	if one.Password != "" {
		if err := one.PasswordMd5(); err != nil {
			bean.Log.Error(err)
			c.String(500, err.Error())
			return
		}

		// 重置token
		if err := one.NewToken(); err != nil {
			bean.Log.Error(err)
			c.String(500, err.Error())
			return
		}
	}

	if req.ID == 0 {
		if err := bean.MysqlDb.Save(&one).Error; err != nil {
			bean.Log.Error(err)
			c.String(500, err.Error())
			return
		}
	} else {
		if err := bean.MysqlDb.Updates(&one).Error; err != nil {
			bean.Log.Error(err)
			c.String(500, err.Error())
			return
		}
	}

	c.String(200, "ok")
}

// 删除管理员
func Delete(c *gin.Context) {
	// 用户输入信息
	var req struct {
		ID int
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		bean.Log.Warning(err)
		c.String(400, err.Error())
		return
	}

	if req.ID == 1 {
		c.String(400, "管理员不能删除")
		return
	}

	var one admin.Admin
	if err := bean.MysqlDb.Where("id=?", req.ID).First(&one).Error; err != nil {
		bean.Log.Warning(err)
		c.String(400, "没有该用户")
		return
	}

	if err := bean.MysqlDb.Delete(&one).Error; err != nil {
		bean.Log.Warning(err)
		c.String(500, "内部错误")
		return
	}

	c.String(200, "ok")
}

func ChangePassword(c *gin.Context) {
	// 用户输入信息
	var req struct {
		Password    string
		NewPassword string
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		bean.Log.Error(err)
		c.String(400, err.Error())
		return
	}

	// 获取登录用户对象
	var one admin.Admin
	if err := one.Get(c); err != nil {
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	}

	// 新旧密码不能相同
	if req.Password == req.NewPassword {
		c.String(400, "新旧密码相同")
		return
	}

	// 验证旧密码
	if s, err := admin.Md5(req.Password); err != nil {
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	} else {
		if s != one.Password {
			c.String(400, "密码错误")
			return
		}
	}

	// 加密新密码
	var str = req.NewPassword
	if s, err := admin.Md5(str); err != nil {
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	} else {
		one.Password = s
	}

	if err := bean.MysqlDb.Save(&one).Error; err != nil {
		c.String(500, err.Error())
		return
	}

	c.String(200, "ok")
}
